<template>
  <div>

    <!-- 评论列表  :class=“{}”动态clas-->
    <div class="cmt-list" :class="{'art-cmt-container-1': isShowCmtInput, 'art-cmt-container-2':!isShowCmtInput}">
        <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
        :immediate-check="false"
      >
      <!-- 评论的 Item 项 -->
      <div class="cmt-item" v-for="(item, index) in list" :key="index">
        <!-- 头部区域 -->
        <div class="cmt-header">
          <!-- 头部左侧 -->
          <div class="cmt-header-left">
            <img :src="item.aut_photo" alt="" class="avatar">
            <span class="uname">{{ item.aut_name }}</span>
          </div>
          <!-- 头部右侧 -->
          <div class="cmt-header-right">
            <van-icon name="like" size="16" v-if="item.is_liking"  color="red" @click=likeFN(true,item) />
            <van-icon name="like-o" size="16" v-else color="gray" @click=likeFN(false,item) />
          </div>
        </div>
        <!-- 主体区域 -->
        <div class="cmt-body">
          {{ item.content }}
        </div>
        <!-- 尾部区域 -->
        <div class="cmt-footer">{{ timeFormate(item.pubdate) }}</div>
      </div>
    </van-list>
    </div>

    <div>
          <!-- 底部添加评论区域 - 1 -->
      <div class="add-cmt-box van-hairline--top"  v-if="isShowCmtInput">
          <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
          <div class="ipt-cmt-div" @click="isShowCmtInput = false">发表评论</div>
          <div class="icon-box">
              <van-badge :content="totalCount === 0 ? '' : totalCount" max="99">
                <!-- :badge="list.length" -->
                  <van-icon   name="comment-o" size="0.53333334rem" @click="commentClickFN"/>
              </van-badge>
              <!-- this.$parent.artObj.is_collected -->
              <van-icon name="star" v-if="$parent.artObj.is_collected" color="red" @click="starFN(true)"   size="0.53333334rem" />
              <van-icon name="star" v-else  @click="starFN(false)"  size="0.53333334rem" />
              <!-- <van-icon name="share-o" size="0.53333334rem" /> -->
          </div>
      </div>

      <!-- 底部添加评论区域  绑定获取焦点和失去焦点事件- 2 -->
      <div class="cmt-box van-hairline--top" v-else>
          <textarea v-model.trim="comText"  placeholder="友善评论、理性发言、阳光心灵" v-fofo  @blur="blurFN"></textarea>
          <van-button type="default" :disabled="comText.length === 0 ? true : false" @click="submitFN">发布</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import { commentsListAPI, responseCommentsList, unresponseCommentsList, sendCommentsAPI, collectionArticleAPI, cancelCollectionArticleAPI } from '@/api/index.js'
import { timeAgo } from '@/utils/date.js'
export default {
  data() {
    return {
      offset: null, // 偏移量的评论id
      list: [], // 评论列表
      isShowCmtInput: true, // 控制textarea是否应该出现
      totalCount: 0, // 评论总数量
      comText: '', // 发布评论内容
      loading: false,
      finished: false,
      offSet: null
    }
  },
  methods: {
    timeFormate: timeAgo,
    // 获取评论列表
    async getCommentsList() {
      const { data: res } = await commentsListAPI({
        art_id: this.$route.query.art_id
      })
      this.list = res.data.results
      this.totalCount = res.data.total_count
      // console.log(res)
      if (res.data.results.length === 0) this.finished = true // 如果没有评论就显示文字
    },

    // 点赞
    async likeFN(bool, item) {
      if (bool) {
        item.is_liking = false
        const res = await unresponseCommentsList(item.com_id)
        console.log(res)
      } else {
        item.is_liking = true
        const res = await responseCommentsList(item.com_id)
        console.log(res)
      }
    },

    // 评论按钮点击事件---第一条评论滑动到最上面
    commentClickFN() {
      // 获取文章内容高度就可以  this.$parent 可以获取到父组件中的绝大部分内容
      // this.$parent.$refs 可以直接获取到父组件的元素内容
      // const contentHeight = this.$parent.$refs.artContent.scrollHeight // 获取文章内容高度
      // window.scrollTo(0, contentHeight) // 让网页进行滚动,设置目标进行滚动
      // .scrollIntoView({
      //      behavior: 'smooth'
      //  })
      // console.log(contentHeight)
      // this.$parent.$refs.artContent.scrollIntoView({
      //   behavior: 'smooth'
      // })

      // 动画平滑效果 scrollIntoView带滚动也带动画  让原生的标签滚动到屏幕最上面
      // 为什么选择likebox点赞部分，是因为如果定位到评论区域，头部导航会挡住评论  (兼容性不高，可能遇到没有动画效果)
      this.$parent.$refs.likeBox.scrollIntoView({
        behavior: 'smooth'
      })
      // console.log(this)
    },

    // 输入框失去焦点事件(防抖) 在输入评论后没来的即提交就被v-if动态删除了，所以来个setTimeout延时销毁(最后来执行实现-异步操作)
    blurFN() {
      setTimeout(() => {
        this.isShowCmtInput = true
      }, 100)
    },

    // 发布按钮的点击事件
    async submitFN() {
      // console.log(this.comText)
      const res = await sendCommentsAPI({
        art_id: this.$parent.artObj.art_id,
        content: this.comText
      })
      this.list.unshift(res.data.data.new_obj) // 使用数组拼接的方式，在评论加载更多的时候进行评论不会报错
      this.totalCount++
      // this.getCommentsList() // 使用接口的方式，在评论加载更多中评论会报错，要注意
      // console.log(res)
      this.comText = ''
      this.commentClickFN() // 返回到刚输入的评论,第一条评论返回到屏幕上面
    },

    // 加载更多
    async onLoad() {
      const res = await commentsListAPI({
        art_id: this.$route.query.art_id,
        offset: this.offSet
      })

      this.list = [...this.list, ...res.data.data.results]
      this.totalCount = res.data.data.total_count
      this.offSet = res.data.data.last_id
      if (res.data.data.last_id === null) { this.finished = true }
      this.loading = false
    },

    // 收藏事件
    async starFN(bool) {
      if (bool) {
        this.$parent.artObj.is_collected = false
        const { data: res } = await cancelCollectionArticleAPI(this.$route.query.art_id)
        console.log(res)
      } else {
        this.$parent.artObj.is_collected = true
        const { data: res } = await collectionArticleAPI(this.$route.query.art_id)
        console.log(res)
      }
    }
  },
  created() {
    // console.log(this.$route)
    this.getCommentsList()
  }
}
</script>

<style scoped lang="less">
.cmt-list {
  padding: 20px;
  // padding-bottom: 2.133333333333333rem;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
    }
  }
}
/*美化 - 文章详情 - 底部的评论页面 */
   // 外层容器
   .art-cmt-container-1 {
     padding-bottom: 46px;
   }
   .art-cmt-container-2 {
     padding-bottom: 80px;
   }

   // 发布评论的盒子 - 1
   .add-cmt-box {
     position: fixed;
     bottom: 0;
     left: 0;
     width: 100%;
     box-sizing: border-box;
     background-color: white;
     display: flex;
     justify-content: space-between;
     align-items: center;
     height: 46px;
     line-height: 46px;
     padding-left: 10px;
     .ipt-cmt-div {
       flex: 1;
       border: 1px solid #efefef;
       border-radius: 15px;
       height: 30px;
       font-size: 12px;
       line-height: 30px;
       padding-left: 15px;
       margin-left: 10px;
       background-color: #f8f8f8;
     }
     .icon-box {
       width: 40%;
       display: flex;
       justify-content: space-evenly;
       line-height: 0;
     }
   }

   .child {
     width: 20px;
     height: 20px;
     background: #f2f3f5;
   }

   // 发布评论的盒子 - 2
   .cmt-box {
     position: fixed;
     bottom: 0;
     left: 0;
     width: 100%;
     height: 80px;
     display: flex;
     justify-content: space-between;
     align-items: center;
     font-size: 12px;
     padding-left: 10px;
     box-sizing: border-box;
     background-color: white;
     textarea {
       flex: 1;
       height: 66%;
       border: 1px solid #efefef;
       background-color: #f8f8f8;
       resize: none;
       border-radius: 6px;
       padding: 5px;
     }
     .van-button {
       height: 100%;
       border: none;
     }
   }
</style>
